﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OS.BLL;
using System.IO;
using Newtonsoft.Json;
using System.Collections;

namespace DownloadPlug_ins
{
    public partial class Picture : Form
    {
        public Common2 common = new Common2();
        public static string url = "";
        public Picture()
        {
            InitializeComponent();
        }

        private void gbtnQuery_Click(object sender, EventArgs e)
        {
            Bll_sptp bllSptp = new Bll_sptp();

            string spbh = this.ctxtSpbh.Text.Trim();
            sptp_Entity entity = bllSptp.GetModel(spbh);

            if (entity != null)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                pictureBox1.Image = Image.FromStream(new MemoryStream(entity.tp));
            }
            else
            {
                MessageBox.Show("该商品编号不存在！");
            }
        }

        private void gbtnDownLoad_Click(object sender, EventArgs e)
        {
            ConfigModel model = common.GetXml();//获取相关配置属性

            int type = 1;
            string spbhs = this.ctxtSpbh.Text;

            url = model.WebService;
            ArrayList parms = new ArrayList();
            Bll_sptp bllSptp = new Bll_sptp();

            string reslut = "";

            int count = 0;

            if (spbhs != "")
            {
                string[] ls = spbhs.Split(',');
                foreach (string s in ls)
                {
                    try
                    {
                        parms.Clear();

                        parms.Add(s);

                        reslut = DSWebService.InvokeWebService(url, "DownloadSpdaTP", parms.ToArray());
                        if (reslut != "-2")
                        {
                            //下载成功

                            sptp_Entity item = JsonConvert.DeserializeObject<sptp_Entity>(reslut);

                            if (!bllSptp.Exists(item.spbh))
                            {
                                if (bllSptp.Add(item))
                                    count++;
                            }
                            else
                            {
                                if (bllSptp.Update(item))
                                    count++;
                            }
                            LogHelper.m_CreateDataLogTxt("DownloadGetTP", "商品编号：" + s, "完成下载，完成时间：" + DateTime.Now.ToString());

                        }
                        else
                        {
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        LogHelper.m_CreateErrorLogTxt("DownloadGetTP", "商品编号：" + s, "错误信息：" + ex.Message);

                    }
                }
            }


            if (count > 0)
            {
                if (type == 1)
                    MessageBox.Show(" 下载完成！ 共计下载数据" + count + "条");
            }
            else
            {
                if (type == 1)
                    MessageBox.Show(" 本时间段未获取到数据，可能网络原因，可能本时间段无更新数据！ ");
            }
        }
    }
}
